<?php

/**
 * 回款业绩仓库
 * @author wangwx 2015-5-12
 */

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\repositories\common\MyscrmContractSyncDeleteDataRepository;
use common\support\StringHelper;
use common\entities\myhtsq\PaymentAchievementEntity;
use common\models\myhtsq\PaymentAchievementModel;

class PaymentAchievementRepository extends BaseRepository
{

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = [])
    {
        parent::__construct($config);
    }

    /**
     * 根据合同id,获取合同回款业绩列表
     * @param string $contractId 合同id
     * @return array
     */
    public static function getPaymentAchievementListByContractId($contractId)
    {
        return PaymentAchievementEntity::find()->where(['contract_id' => $contractId])->asArray()->all();
    }

    /**
     * 根据回款签约id,删除回款业绩
     * @param string $achievementId 签约id
     * @return boolean
     */
    public static function deletePaymentAchievementByAchievementId($achievementId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteResult = PaymentAchievementEntity::deleteAll(['achievement_id' => $achievementId]);

        if ($deleteResult > 0) {
            $contractSyncDeleteDataRepository->add('myscrm_payment_achievement', 'achievement_id', $data['achievement_id']);
        }

        return $deleteResult;
    }

    /**
     * 新增回款业绩
     * @param common\models\myhtsq\PaymentAchievementModel $paymentAchievementModel 回款业绩模型
     * @return boolean|string
     */
    public static function addPaymentAchievement(PaymentAchievementModel $paymentAchievementModel)
    {
        $paymentAchievementEntity = new PaymentAchievementEntity();

        $data = $paymentAchievementModel->attributes;
        $paymentAchievementEntity->load($data, '');
        $paymentAchievementEntity->achievement_id = StringHelper::uuid();
        if ($paymentAchievementEntity->save()) {
            return $paymentAchievementEntity->achievement_id;
        } else {
            return false;
        }
    }

    /**
     * 更新回款业绩
     * @param common\models\myhtsq\PaymentAchievementModel $paymentAchievementModel 回款业绩模型
     * @return string|boolean
     */
    public static function updatePaymentAchievement($paymentAchievementModel)
    {
        $paymentAchievementEntity = new PaymentAchievementEntity();
        $paymentAchievementModelInfo = $paymentAchievementEntity::find()->where(['achievement_id' => $paymentAchievementModel->achievement_id])->one();
        $data = $paymentAchievementModel->attributes;
        $paymentAchievementModelInfo->load($data, '');
        if ($paymentAchievementModelInfo->save()) {
            return $paymentAchievementModelInfo->achievement_id;
        } else {
            return false;
        }
    }

    /**
     * 根据回款与发票及应收款对应关系ID,删除回款业绩
     * @param string $invoicePaymentId 回款与发票及应收款对应关系ID
     * @return boolean|int
     */
    public static function deletePaymentAchievementByInvoicePaymentId($invoicePaymentId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteData = PaymentAchievementEntity::findAll(['invoice_payment_id' => $invoicePaymentId]);

        $deleteResult = PaymentAchievementEntity::deleteAll(['invoice_payment_id' => $invoicePaymentId]);

        if($deleteResult > 0){
            foreach ($deleteData as $data){
                $contractSyncDeleteDataRepository->add('myscrm_payment_achievement', 'achievement_id', $data['achievement_id']);
            }
        }

        return $deleteResult;
    }

    /**
     * 删除回款业绩
     * @param string $paymentId 回款ID
     * @return boolean|int
     */
    public static function deletePaymentAchievementByPaymentId($paymentId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteData = PaymentAchievementEntity::findAll(['payment_id' => $paymentId]);

        $deleteResult = PaymentAchievementEntity::deleteAll(['payment_id' => $paymentId]);

        if($deleteResult > 0){
            foreach ($deleteData as $data){
                $contractSyncDeleteDataRepository->add('myscrm_payment_achievement', 'achievement_id', $data['achievement_id']);
            }
        }

        return $deleteResult;
    }


    /**
     * 根据合同id删除
     * @param $contractId
     * @return int
     */
    public static function deleteByContractId($contractId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteData = PaymentAchievementEntity::findAll(["contract_id" => $contractId]);

        $deleteResult = PaymentAchievementEntity::deleteAll(["contract_id" => $contractId]);

        if ($deleteResult > 0) {
            foreach ($deleteData as $data) {
                $contractSyncDeleteDataRepository->add('myscrm_payment_achievement', 'achievement_id', $data['achievement_id']);
            }
        }

        return $deleteResult;
    }

}
